package com.mfnote.view.write;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.mfnote.adapter.CheckBoxAdapter;
import com.mfnote.adapter.CheckBoxAdapter.ViewHolder;
import com.mfnote.db.dao.WriteDao;
import com.mfnote.view.R;
import com.mfnote.view.R.anim;
import com.mfnote.view.R.id;
import com.mfnote.view.R.layout;
import com.mfnote.view.other.MyApplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
/**
 * 批量删除界面
 * @author ZHF
 *
 */
public class WriteBatchDeleteActivity extends Activity {
	public static final String TAG = "WriteBatchDeleteActivity";
	
	ListView lv_batchDel = null;
	CheckBox cb = null;
	Button mBtnDelete, mBtnBack, mBtnAllSelect;
	
	ArrayList<HashMap<String,Object>> data; //接收数据从列表
	ArrayList<HashMap<String,Object>> tempData; //加入flag参数
	private int checkNum; // 记录选中的条目数量

	
	CheckBoxAdapter mAdapter; //自定义适配器
	
	WriteDao dao = null; //数据库操作类
	
	TextView tv_selectednum;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.writebatchdelete);
		
		MyApplication.getInstance().addActivity(this);
		tv_selectednum = (TextView) this.findViewById(R.id.tv_selectednum);
		
		//接收数据从列表
		Intent intent = getIntent();
	    data = (ArrayList<HashMap<String, Object>>) intent.getExtras().get("allwrites");
	  
	    //给每一项加入flag
	    initCheckBoxState();
	    
//		Log.i(TAG, tempData.toString());
		
		//实例化控件
		lv_batchDel = (ListView) findViewById(R.id.writebatchdelete_lv_batchdelete);
		mBtnDelete = (Button) findViewById(R.id.writebatchdelete_btn_delete);  //删除
		mBtnBack = (Button) findViewById(R.id.writebatchdelete_btn_back);  //返回
		mBtnAllSelect = (Button) findViewById(R.id.writebatchdelete_btn_allSelect);  //全选
		
		dao = new WriteDao(this);//实例化数据库操作类
		
		mAdapter = new CheckBoxAdapter(tempData, this) ; //往适配器里加的数据是含有flag的
		lv_batchDel.setAdapter(mAdapter);  //自定义适配器
		//给listView绑定监听器
		lv_batchDel.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
//				   Toast.makeText(WriteBatchDeleteActivity.this, "当前位置：" + position,
//	                        Toast.LENGTH_SHORT).show();
				
				// 取得ViewHolder对象，这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
				ViewHolder holder = (ViewHolder) view.getTag();
				//改变CheckBox的状态
				holder.cb.toggle();
				//将CheckBox的选中状态记录下来
				//调整选定条目
				if(holder.cb.isChecked() == true) {
					tempData.get(position).put("flag", "true");
					checkNum ++;
				} else {
					tempData.get(position).put("flag", "false");
					checkNum --;
				}
				//用TextView显示
				tv_selectednum.setText("已选中" +checkNum+ "项");
			}
		});
		
		//确认删除的回调接口
		mBtnDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Iterator<HashMap<String, Object>> iterator = tempData.iterator();
				while (iterator.hasNext()) {
					HashMap<String, Object> temp = iterator.next();
					if(temp.get("flag").equals("true")) {
						iterator.remove(); //将以选中项从列表中移除掉
						dao.deleteWrite(temp);  //根据集合map<String,Object>中title删除该项数据
					}
					checkNum = 0;
					//通知列表数据修改
					datachanged();
				}
			}
		});
		
		// 全选按钮的回调接口
		mBtnAllSelect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(mBtnAllSelect.getText().equals("全选")) {
					//遍历list的长度，将MyAdapter中的map值全部设置为true
					for (int i = 0; i < tempData.size(); i++) {
						tempData.get(i).put("flag", "true");
					}
					mBtnAllSelect.setText("反选");
					checkNum = tempData.size(); //数量设置为list的长度
					datachanged(); //更新数据，刷新界面
				} else {
					//遍历list的长度，将MyAdapter中的map值全部设置为false
					for (int i = 0; i < tempData.size(); i++) {
						tempData.get(i).put("flag", "false");
					}
					mBtnAllSelect.setText("全选");
					//tv_selectednum.setText("已选中0项");
					checkNum = 0; //数量设置为list的长度
					datachanged(); //更新数据，刷新界面
				}
				
			}
		});
		//返回按钮
		mBtnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WriteBatchDeleteActivity.this,WriteActivity.class);
				setResult(1);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.push_left_in,
						R.anim.push_right_out);
			}
		});
		
	}
	
	/**初始化checkBox状态flag**/
	private void initCheckBoxState() {
		Iterator<HashMap<String, Object>> itr = data.iterator();
		tempData = new ArrayList<HashMap<String,Object>>();
		while (itr.hasNext()) {
			HashMap<String, Object> temp = itr.next();
			//排除首行数据和加锁数据哦
			if(!temp.get("title").equals("新建记事说明") && Integer.parseInt(temp.get("lock").toString()) != 1) {
				temp.put("flag", "fasle");
				tempData.add(temp);
			}
		}
	}
	
	/**更新数据**/
	private void datachanged() {
		// 通知listView进行刷新
		mAdapter.notifyDataSetChanged();
		
		//显示项数
		tv_selectednum.setText("已选中" + checkNum + "项");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		Intent intent = new Intent(WriteBatchDeleteActivity.this,WriteActivity.class);
		setResult(1);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.push_left_in,
				R.anim.push_right_out);
		
		return super.onKeyDown(keyCode, event);
	}
	
}
